﻿using Common.Attributes;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Admin
{
    public class AuditController : Controller
    {
        #region [HttpPost]审核通过方法 + AuditCompany(MODEL.qgzx_admin modelAdmin)
        /// <summary>
        /// 审核通过方法
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AuditCompany(MODEL.qgzx_admin modelAdmin)
        {
            try
            {
                modelAdmin.Id = 0;
                OperateContext.Current.BLLSession.Iqgzx_adminBLL.Add(modelAdmin);

                int id = int.Parse(Request.Form["id"]);
                MODEL.qgzx_company model = OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetListBy(x => x.CompanyId == id).First();
                if(model.IsAudit == 0)
                {
                    model.IsAudit = 1;
                    OperateContext.Current.BLLSession.Iqgzx_companyBLL.ModifyBy(model, x => x.CompanyId == id, "IsAudit");
                    return OperateContext.Current.RedirectAjax("ok", "审核通过成功~", null, "");
                }
                if (model.IsAudit == 1)
                {
                    return OperateContext.Current.RedirectAjax("ok", "该单位已经通过审核~", null, "");
                }
                else
                {
                    model.IsAudit = 2;
                    OperateContext.Current.BLLSession.Iqgzx_companyBLL.ModifyBy(model, x => x.CompanyId == id, "IsAudit");
                    return OperateContext.Current.RedirectAjax("ok", "审核通过成功~", null, "");
                }
                
               
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "出错了~~~~~~", null, "");
            }
        }
        #endregion


        #region 在这审核通过 加载添加管理员  html代码 + [HttpGet] AuditCompany(int id)
        /// <summary>
        /// 在这新增管理员 html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AuditCompany(int id)
        {
            //OperateContext.Current.BLLSession.Iqgzx_adminBLL.GetListBy(p => p.Id != null);

            MODEL.qgzx_company model= OperateContext.Current.BLLSession.Iqgzx_companyBLL.GetListBy(x => x.CompanyId == id).First();

            MODEL.ViewModel.Admin admin = new MODEL.ViewModel.Admin();
            admin.LoginName = model.CompanyName;
            if(model.IsAudit==0)
            {
                MODEL.qgzx_admin a = OperateContext.Current.BLLSession.Iqgzx_adminBLL.GetListBy(x=>x.LoginName==model.LoginName).FirstOrDefault();
                OperateContext.Current.BLLSession.Iqgzx_adminBLL.Del(a);
                model.IsAudit = 1;
                OperateContext.Current.BLLSession.Iqgzx_companyBLL.ModifyBy(model, x => x.CompanyId == id, "IsAudit");
                return OperateContext.Current.RedirectAjax("ok", "审核通过成功~", null, "");
            }
            if(model.IsAudit==1)
            {
               return OperateContext.Current.RedirectAjax("ok", "已经通过审核", null, "");
            }
            else
            {
                return PartialView("EditAdmin", admin);
            }
            
        }
        #endregion


        #region [HttpPost]审核通过方法 +  AuditArticle(int id)
        /// <summary>
        /// 审核文章通过方法
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AuditArticle(int id)
        {
            try
            {
                MODEL.qgzx_article model = OperateContext.Current.BLLSession.Iqgzx_articleBLL.GetListBy(x => x.ArticleId == id).First();
                if(model.IsAudit==1)
                {
                    model.IsAudit = 2;
                }
                else
                {
                    model.IsAudit = 1;
                }
                OperateContext.Current.BLLSession.Iqgzx_articleBLL.ModifyBy(model, x => x.ArticleId == id, "IsAudit");
                return OperateContext.Current.RedirectAjax("ok", "审核成功~", null, "");
            }
            catch (Exception ex)
            {
                return OperateContext.Current.RedirectAjax("err", "出错了~~~~~~", null, "");
            }
        }
        #endregion
    }
}
